﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zocono.WCS.Domain.DomainRepository;
using Zocono.WCS.Domain.EntityInfo;
using AutoMapper;
using Zocono.Infrastructure.Common;
using System.Linq.Expressions;
using Zocono.WCS.Infrastructure.ApllicationDto.DtoWcsManage;

namespace Zocono.WCS.Domain.UserManageDomain
{
    public class MenuInfoService : IMenuInfoService
    {
        private IMenuInfoRepository _MenuInfoRepository;
        private IMapper _Mapper;
        private readonly ISnowflake _snowflake;

        public MenuInfoService(IMenuInfoRepository menuInfoRepository,IMapper mapper, ISnowflake snowflake)
        {
            _MenuInfoRepository = menuInfoRepository;
            _Mapper = mapper;
            _snowflake = snowflake;
        }

        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="menuInfoDto"></param>
        /// <returns></returns>
        public async Task<bool> AddMenuAsync(MenuInfoDto menuInfoDto)
        {
            MenuInfo menuInfo=new MenuInfo();
            _Mapper.Map(menuInfoDto,menuInfo);
            menuInfo.MenuID=_snowflake.GetId();
            return await _MenuInfoRepository.AsSugarClient().Insertable(menuInfo).IgnoreColumns("Name","ParentID").ExecuteCommandIdentityIntoEntityAsync();
        }

        public async Task<bool> UpdateMenuInfoAsync(MenuInfoDto menuInfoDto)
        {
            return await _MenuInfoRepository.UpdateAsync(it => new MenuInfo()
            {
                MenuName = menuInfoDto.MenuName,
                MenuTag = menuInfoDto.MenuTag,
                SonID = menuInfoDto.SonID,
            },
            it => it.MenuID == menuInfoDto.MenuID);
        }

        /// <summary>
        /// 添加用户时获取菜单
        /// </summary>
        /// <returns></returns>
        public async Task<List<MenuInfoDto>> GetMenuAsync()
        {
            List<MenuInfoDto> menuInfoDto = new List<MenuInfoDto>();
            var result = await _MenuInfoRepository.GetListAsync();
            _Mapper.Map(result,menuInfoDto);
            return menuInfoDto;
        }

        /// <summary>
        /// 获取菜单
        /// </summary>
        /// <returns></returns>
        public async Task<MenuInfoDto> GetMenuByIDAsync(long menuID)
        {
            MenuInfoDto menuInfoDto = new MenuInfoDto();
            var res=await _MenuInfoRepository.GetSingleAsync(x=>x.MenuID==menuID);
            _Mapper.Map(res, menuInfoDto);
            return menuInfoDto;
        }

        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="func"></param>
        /// <returns></returns>
        public async Task<bool> DeleteMenuAsync(Expression<Func<MenuInfo, bool>> func)
        {
            return await _MenuInfoRepository.DeleteAsync(func);
        }
    }
}
